package com.gba.mark.exam.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gba.mark.exam.entity.ExamPaper;
import com.gba.mark.exam.entity.ExamQuestions;
import com.gba.mark.exam.mapper.ExamPaperMapper;
import com.gba.mark.exam.mapper.ExamQuestionsMapper;
import com.gba.mark.exam.service.ExamQuestionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import springfox.documentation.spring.web.json.Json;

import javax.annotation.Resource;
import java.io.Serializable;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class ExamQuestionsImpl extends ServiceImpl<ExamQuestionsMapper, ExamQuestions> implements ExamQuestionsService {

    @Resource
    private ExamQuestionsMapper examQuestionsMapper;

    @Resource
    private ExamPaperMapper examPaperMapper;

    @Autowired
    private ExamQuestionsService examQuestionsService;



    /**
     *新建并保存一个题目
     */

    @Override
    public Integer saveQuestion(ExamQuestions examQuestions) {

        return this.baseMapper.insert(examQuestions);

    }


    //    /**
//    *接收前端Question的list,获得它的内容包括题目以及答案，然后保存到数据库(新建)
//     */
//    @Override
//    public List<ExamQuestions> receiveData(List<ExamQuestions> list){
//
//        for(ExamQuestions examQuestions : list){
//
//            examQuestions.setQuestions(examQuestions.getQuestions());
//            examQuestions.setId(examQuestions.getId());
//            examQuestions.setAnswer(examQuestions.getAnswer());
//
//         examQuestionsService.save(examQuestions);
//
//        }
//
//            return list;
//
//    }


//    public List<ExamQuestions>    receiveData(Integer group_id,List<ExamQuestions> list)
//
//    for(ExamQuestions examQuestions : list){
//
//        Json id = examQuestionsMapper.insert(examQuestions);
//        ExamPaper examPaper = new ExamPaper();
//        examPaper.setQuestion_ids(id);
//        examPaper.setGroup_id(group_id);
//
//        examPaperMapper.insert(examPaper);

    @Transactional(
            rollbackFor =  {Exception.class}
    )

    /**
    *删除题目
     */

    @Override
    public boolean removeById(Serializable id){

        //删除对应的题目类型数据
        ExamQuestions baseQuestion = this.baseMapper.selectById(id);

        //当删除题目时也从试卷表中删除相应的内容
        examQuestionsService.remove(new QueryWrapper<ExamQuestions>().eq("id",String.valueOf(id)));
        return true;

    }


    @Transactional(
            rollbackFor =  {Exception.class}
    )
    /**
    *查询题目
     */

    @Override

    public List<ExamQuestions> findQuestion(String questions, Integer group_id, Object question_ids) {

         return examQuestionsMapper.findQuestion(questions,group_id,  question_ids);

    }


}